package com.zzptc.sky.bdsjws.services;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zzptc.sky.bdsjws.MyApplication;
import com.zzptc.sky.bdsjws.R;
import com.zzptc.sky.bdsjws.activites.UpdateActivity;
import com.zzptc.sky.bdsjws.utils.MyUtil;

import java.io.File;

import okhttp3.Call;

public class MyDownLoadService extends Service {
    NotificationManager notificationManager;
    NotificationCompat.Builder    notificationBuild;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String url=intent.getStringExtra("apkurl");
        createNotification();
        File file=MyUtil.getDirs();
        downloadfile(url,file);//两个参数分别表示：1.从哪取 2.放哪
        return super.onStartCommand(intent, flags, startId);
    }

    public void createNotification(){
        notificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationBuild = new NotificationCompat.Builder(MyApplication.getContext());
        notificationBuild.setContentTitle("下载中……")
                .setContentText("准备下载……")
                .setSmallIcon(R.mipmap.logo_small)
                .setOngoing(true) //设置他为一个正在进行的通知
                .setAutoCancel(false);
        notificationManager.notify(0, notificationBuild.build());

    }
    //安装方法
//    private void installAPK(File file){
//
//       // MyApplication.getContext().startService(intent);
//    }
    private void downloadfile(String url, final File file) {

        //使用OKHttp
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new FileCallBack(file.getAbsolutePath(),"baidu.apk") {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        notificationBuild.setContentText("下载失败");
                        notificationManager.notify(0,notificationBuild.build());
                    }

                    @Override
                    public void onResponse(File file1, int id) {
                        notificationBuild.setOngoing(false);
                        notificationBuild.setAutoCancel(true);
                        notificationBuild.setContentText("下载完毕");
                        notificationManager.notify(id, notificationBuild.build());
                        //调用安装的方法安装
                        //得到一个Intent
//                        installAPK(file);
                        Intent intent=MyUtil.getInstall(file);
                        TaskStackBuilder stackBuilder = TaskStackBuilder.create(MyDownLoadService.this);
                        stackBuilder.addParentStack(UpdateActivity.class);
                        stackBuilder.addNextIntent(intent);
                        PendingIntent resultPendingIntent= stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
                        notificationBuild.setContentIntent(resultPendingIntent);
                        notificationBuild.setProgress(0,0,false);
                        notificationBuild.setContentText("点击安装");
                        notificationBuild.setContentTitle("下载完毕");
                        notificationManager.notify(0,notificationBuild.build());
                    }

                    @Override
                    public void inProgress(float progress, long total, int id) {
                        super.inProgress(progress, total, id);
                        notificationBuild.setProgress(100,(int)(progress*100),true);
                        notificationBuild.setContentText("已经下载了"+(int)(progress*100)+"%");
                        notificationManager.notify(id,notificationBuild.build());
                    }
                });
    }


}
